Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jira/coin 1151: Tezos: rework getting balance for originated accounts #679

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

hlafet-ledger
Copy link
Contributor

No description provided.

@hlafet-ledger hlafet-ledger changed the title Jira/coin 1151: rework getting balance for originated accounts Jira/coin 1151: Tezos: rework getting balance for originated accounts Dec 17, 2020
}

void TezosLikeWallet::invalidateBalanceCache(size_t accountIndex, const std::string& originatedAccount) {
getBalanceCache().erase(fmt::format("{}-{}-{}", getCurrency().name, accountIndex, originatedAccount));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe extract the format in a constant to make sure it's always formatted the same?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I will create a single function to format the cache key

auto tzOp = op->asTezosLikeOperation();
auto tzTx = tzOp->getTransaction();
auto value = (tzTx->getType() == api::TezosOperationTag::OPERATION_TAG_TRANSACTION)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could just not call update_balance when that was not a OPERATION_TAG_TRANSACTION?
Since we know this will not change the sum anyway

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update_balance is called by a generic code (shared between several coins), we cannot use tezos specific functions (as getType)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Then maybe quit the function asap?

if (tzTx->getType() != api::TezosOperationTag::OPERATION_TAG_TRANSACTION)
    return;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sum can be impacted by fees (even if value is equal to 0). We cannot quit the function

if (!localAccount) {
throw make_exception(api::ErrorCode::NULL_POINTER, "Account was released.");
}
auto wallet = std::dynamic_pointer_cast<TezosLikeWallet>(localAccount->getWallet());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should check for null pointer here to avoid segfaults later

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only case that I can see to get a sigfault here is to have a TezosLikeAccount inside a wallet different from a TezosLikeWallet, which is not possible.
I know that we should check for null after a dynamic cast when we are not sure about type matching. But here we are sure about the pointer type.

Copy link
Contributor

@gagbo gagbo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks fine, I just don't understand:

  • when the balanceCache is supposed to be invalidated ?
  • TTLCache means Time To Live Cache ? Where is the duration for this cache defined ?

@@ -115,7 +115,7 @@ namespace ledger {
const std::vector<std::string> &matchableKeys);

virtual Future<std::shared_ptr<BigInt>>
getBalance(const std::vector<TezosLikeKeychain::Address> &addresses) = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could keep TezosLikeKeychain::Address instead of std::string to keep some more type-safety?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1- As you can see for all other functions in this class, address was always a string.
2- the old format was using a std::vector which must contais only one element (else an error)
So to keep the code simple and consistent, I changed the signature of this function

@hlafet-ledger
Copy link
Contributor Author

It looks fine, I just don't understand:

  • when the balanceCache is supposed to be invalidated ?
  • TTLCache means Time To Live Cache ? Where is the duration for this cache defined ?

It looks fine, I just don't understand:

  • when the balanceCache is supposed to be invalidated ?
  • TTLCache means Time To Live Cache ? Where is the duration for this cache defined ?

Yes this is a time to live cache. It is defined in the AbstractWallet class. Duration can be configured through api::Configuration::TTL_CACHE, and default value is 30 seconds

@@ -62,9 +63,11 @@ namespace ledger {
}

static inline void update_balance(std::shared_ptr<api::Operation> const& op, BigInt& sum) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename this function updateBalance for style consistency

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is called through a template call (see getBalanceHistoryFor in core/src/wallet/common/BalanceHistory.h)
If I change the name here, i have to modify it everywhere including in ethereum code.

getBalanceCache().put(fmt::format("{}-{}-{}", getCurrency().name, accountIndex, originatedAccount), balance);
}

void TezosLikeWallet::invalidateBalanceCache(size_t accountIndex, const std::string& originatedAccount) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this method useful? I don't see any references to it in the code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is not called. I added it as a helper function for, may be, a future use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants